package com.kd.controller.recruiter;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.kd.bean.recruiter.Company_msg;
import com.kd.bean.recruiter.Recruiter_basic;
import com.kd.service.recruiter.ICompany_msgService;
import com.kd.service.recruiter.IRecruiter_basicService;

@Controller
@RequestMapping("/company")
public class Company_msgController {

	@Autowired
	private ICompany_msgService companyService;

	@Autowired
	private IRecruiter_basicService recService;

	@RequestMapping("/insertCompanyMsg")
	public String insertCompanyMsg(Company_msg company, HttpSession session,
			@RequestParam("myFile") MultipartFile myFile, HttpServletRequest request, Model model) {

		Recruiter_basic basic = (Recruiter_basic) session.getAttribute("basicMsg");
		Boolean flag = false;
		// 1、设置上传路径
		String storPath = System.getProperty("user.dir") + "/data/tmp/image";
		// 2、判断该位置存在不存在
		File file = new File(storPath);
		if (!file.getParentFile().exists()) {// 如果父路径不存在
			file.mkdirs();// 则创建目录
		}
		try {
			// 3、将文件上传到上传路径
			File file2 = new File(storPath, myFile.getOriginalFilename());
			myFile.transferTo(file2);
			// 给头像地址
			company.setCompany_img(myFile.getOriginalFilename());
			flag = companyService.insertCompanyMsg(company, basic);
			if (flag) {
				model.addAttribute("msg", "补充成功");
				// 补充成功，重新给session赋值
				Recruiter_basic basicMsg = recService.basicLogin(basic);
				session.setAttribute("basicMsg", basicMsg);
				return "/view/job/jobPersonData.jsp";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("msg", "补充失败");
		return "/view/job/jobPersonData.jsp";
	}

	@RequestMapping("/updateCompanyMsg")
	public String updateCompanyMsg(Company_msg company, HttpSession session,
			@RequestParam("myFile") MultipartFile myFile, HttpServletRequest request, Model model) {
		Recruiter_basic basic = (Recruiter_basic) session.getAttribute("basicMsg");
		Boolean flag = false;
		// 1、设置上传路径
		String storPath = System.getProperty("user.dir") + "/data/image";
		// 2、判断该位置存在不存在
		File file = new File(storPath);
		if (!file.getParentFile().exists()) {// 如果父路径不存在
			file.mkdirs();// 则创建目录
		}
		try {
			// 3、将文件上传到上传路径
			File file2 = new File(storPath, myFile.getOriginalFilename());
			myFile.transferTo(file2);
			// 给头像地址
			company.setCompany_img(myFile.getOriginalFilename());
			flag = companyService.updateCompanyMsg(company, basic);
			if (flag) {
				model.addAttribute("msg", "修改成功");
				return "/view/job/jobPersonData.jsp";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("msg", "修改失败");
		return "/view/job/jobPersonData.jsp";
	}
	
	@RequestMapping("/queryByCompany_id")
	@ResponseBody
	public Company_msg queryByCompany_id(HttpSession session) {
		Company_msg company = null;
		Recruiter_basic basic = (Recruiter_basic) session.getAttribute("basicMsg");
		try {
			Recruiter_basic basicMsg = recService.basicLogin(basic);
			company = companyService.queryByCompany_id(basicMsg.getCompany_id());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return company;
	}
}
